<template>
  <div class="search">
    <van-nav-bar fixed title="商品列表" left-arrow @click-left="$router.go(-1)" />

    <van-search
      readonly
      shape="round"
      background="#ffffff"
      :value=" keyword || '搜索商品' "
      show-action
      @click="$router.push('/search')"
    >
      <template #action>
        <van-icon class="tool" name="apps-o" />
      </template>
    </van-search>

    <!-- 排序选项按钮 -->
    <div class="sort-btns">
      <div class="sort-item" @click="changeSortType('all')">综合</div>
      <div class="sort-item" @click="changeSortType('sales')">销量</div>
      <div class="sort-item" @click="changeSortType('price')">价格 </div>
    </div>

    <div class="goods-list">
      <goodsItem v-for="item in proList" :key="item.goods_id" :item="item"></goodsItem>
    </div>
  </div>
</template>

<script>
import { getProList } from '@/api/search'
import goodsItem from '@/components/goodsItem'

export default {
  name: 'SearchList',
  components: {
    goodsItem
  },
  data () {
    return {
      page: 1,
      proList: [],
      status: 1,
      sortType: '',
      sortPrice: 0
    }
  },
  computed: {
    keyword () {
      return this.$route.query.search
    }
  },
  created () {
    this.getProList('all')
  },
  methods: {
    changeSortType (type) {
      this.sortType = type
      if (type === 'price') {
        this.sortPrice = this.sortPrice === 0 ? 1 : 0
        console.log('price:' + this.sortPrice)
      }
      this.getProList()
    },
    async getProList () {
      await getProList({
        // 做了分类页跳转过来的判断
        sortType: this.sortType,
        sortPrice: this.sortPrice,
        categoryId: this.$route.query.categoryId,
        goodsName: this.keyword,
        page: this.page
      }).then(res => {
        const { data: { list } } = res
        this.proList = list.data
      })
    }
  }

}
</script>

<style lang="less" scoped>
.search {
  padding-top: 46px;
  .tool {
    font-size: 24px;
    height: 40px;
    line-height: 40px;
  }

  .sort-btns {
    display: flex;
    height: 36px;
    line-height: 36px;
    .sort-item {
      text-align: center;
      flex: 1;
      font-size: 16px;
    }
  }
}

// 商品样式
.goods-list {
  background-color: #f6f6f6;
}
</style>
